<template>
  <div class="goods">
    <div class="item"
         v-for="(good,index) in goods"
         :key="good.id+'id'">
      <div class="check">
        <input type="checkbox"
               :checked="good.checked"
               @click="sendState(index,$event)" />
      </div>
      <div class="img">
        <img :src="good.goods_img" />
      </div>
      <div class="content">
        <div class="name">{{good.goods_name}}</div>
        <div class="price">
          <div>¥{{good.goods_price}}</div>
          <div class="bn">
            <span @click="cut(good.id,good.goods_count)">-</span>
            <input type="number"
                   class="num"
                   :value="good.goods_count"
                   disabled />
            <span @click="add(good.id)">+</span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import bus from '@/components/bus.js'
export default {
  data () {
    return {
      goods: [],
      sendData: {},
      checked: null
    }
  },
  created () {
    var that = this;
    bus.$on('checkAll', (check) => {
      that.goods.forEach((obj) => {
        obj.checked = check
      })
      this.$forceUpdate()
    })
  },
  updated () {
    console.log("---------updated--------");
  },
  methods: {
    sendState (index, e) {
      this.sendData.index = index
      this.sendData.checked = e.target.checked
      bus.$emit('getData', this.sendData)
    },
    cut (id, count) {
      if (count > 0) {
        this.axios.post('http://localhost:8081/goods/cut', { id: id }).then((res) => {
          if (res.data.msg.code == '2000') {
            this.$parent.isGoods = false;
            this.$emit('queryList');
          }
        });
      }
    },
    add (id) {
      this.axios.request({
        url: 'goods/add', method: 'post', baseURL: 'http://localhost:8081/', data: { id: id },
      }).then((res) => {
        if (res.data.msg.code == '2000') {
          this.$emit('queryList');
        }
      })
    }
  }

}
</script>

<style lang="less" scoped>
.goods {
  margin: 60px 0px;
}
.item {
  padding: 10px 0px;
  height: 100px;
  display: flex;
  border-bottom: 1px solid rgb(222, 225, 230);
}
.check {
  flex: 1;
  // width: 30px;
}
.check > input {
  margin-top: 42px;
  margin-left: 9px;
}
.img > img {
  flex: 3;
  height: 100px;
}
.content {
  //width: 245px;
  flex: 8;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}
.name {
  padding: 0px 10px;
  font-size: 14px;
}
.price {
  padding: 0px 10px;
  display: flex;
  justify-content: space-between;
}
.num {
  width: 10px;
  padding: 2px 10px;
  border: 1px solid rgb(222, 225, 230);
}
.price > div:nth-child(1) {
  margin-bottom: 10px;
  color: rgb(244, 16, 54);
  font-size: 15px;
  // font-weight: bold;
}
.bn {
}
.bn > span {
  padding: 0px 5px;
}
</style>